package com.start.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.manager.dao.UserDao;
import com.start.manager.domain.entity.User;
import com.start.manager.domain.request.LoginParam;
import com.start.manager.enums.ResultCodeEnum;
import com.start.manager.service.IUserService;
import com.start.manager.util.MyUtil;
import com.jienlee.common.exception.BusinessException;
import com.jienlee.common.util.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lijian
 * @since 2019-12-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {

    @Override
    public String login(LoginParam loginParam) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new QueryWrapper<User>().lambda()
                .eq(User::getAccount, loginParam.getAccount());
        User user = this.baseMapper.selectOne(lambdaQueryWrapper);
        AssertUtil.nonNull(user, BusinessException.of(ResultCodeEnum.USER_NOT_FIND));

        if (!StringUtils.equals(user.getPassword(), MyUtil.encoderMD5(loginParam.getPassword()))) {
            throw BusinessException.of(ResultCodeEnum.PASSWORD_ERROR);
        }
        Long expireTime = System.currentTimeMillis() + 423000;
        String content = user.getId().toString() + "_" + expireTime.toString();
        String token = UUID.randomUUID().toString();
        return token;
    }
}
